<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/21
 * Time: 13:44
 */

namespace app\admin\controller;

use app\common\model\ActivityPrize;
use app\common\model\Activity;
use app\common\model\ActivitySponsor;
use app\admin\validate\PrizeValidate;

class Prize extends CheckLogin
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $page = (int)$this->request->post('pageIndex');
            $limit = (int)$this->request->post('pageSize');
            $name = $this->request->post('dataName', '');
            $activityTitle = $this->request->post('dataActivity', 0);
            $sponsor  = $this->request->post('dataSponsor', 0);
            $offset = ($page - 1) * $limit;
            $where = [];
            empty($name) or $where['prize|rank_name'] = ['like', '%' . $name . '%'];
            empty($activityTitle) or $where['aid'] = $activityTitle;
            empty($sponsor) or $where['sponsor'] = $sponsor;
            $activityPrize = new ActivityPrize();
            $totalCount = $activityPrize->where($where)->count();
            $list = $totalCount > 0 ? $activityPrize->allPrize($where, $offset, $limit) : [];

            $data = [
                'code' => 0,
                'msg' => 'success',
                'list' => $list,
                'count' => $totalCount
            ];
            return $this->json($data);
        } else {
            $activity = Activity::where(['status' => 1])->column('id, title', 'id');
            $sponsor = ActivitySponsor::where(['is_del' => 0, 'status' => 1])->column('id, title', 'id');
            $this->assign('activity', $activity);
            $this->assign('sponsor', $sponsor);
            return $this->fetch();
        }
    }

    public function add()
    {
        if($this->request->isPost()) {
            $data = $this->request->post();
            $prizeValidate = new PrizeValidate();
            if($prizeValidate->scene('add')->check($data)){
                $prize = new ActivityPrize();
                unset($data['file']);
                if($prize->add($data) != null){
                    return $this->jsonSuccess('添加成功', ['url'=>'/prize/index']);
                }
                $errMsg = '添加失败';
            }else{
                $errMsg = $prizeValidate->getError();
            }
            return $this->jsonFail($errMsg, ['url'=>'/prize/index']);
        } else {
            $aid = $this->request->param('aid', 0);
            $activity = Activity::underwayActivity([], false);
            $this->assign('aid', $aid);
            $this->assign('activity', $activity);
            return $this->fetch();
        }
    }

    public function edit()
    {
        if($this->request->isPost()) {
            $data = $this->request->post();
            $prizeValidate = new PrizeValidate();
            if($prizeValidate->scene('edit')->check($data)){
                $prize = new ActivityPrize();
                unset($data['file']);
                if($prize->edit($data) != null){
                    return $this->jsonSuccess('修改成功', ['url'=>'/prize/index']);
                }
                $errMsg = '修改失败';
            }else{
                $errMsg = $prizeValidate->getError();
            }
            return $this->jsonFail($errMsg, ['url'=>'/prize/index']);
        } else {
            $id = $this->request->param('id');
            $row = ActivityPrize::get($id);

            $activity = Activity::underwayActivity([], false);
            $this->assign('activity', $activity);
            $this->assign('row', $row);
            return $this->fetch();
        }
    }

    public function del()
    {
        $id = (int)$this->request->post('id');
        $prize = new ActivityPrize();
        if($prize->del($id)){
            return $this->jsonSuccess('删除成功');
        }
        return $this->jsonFail('删除失败');
    }
}
